﻿using System.Web;
using Ayatta.Contract;
using Enyim.Caching;
using Web.My.Models;
using System.Web.Mvc;
using Web.Base.Helpers;
using Web.Base.Security;
using Ayatta.Common.Extensions;
using Ayatta.Contract.Component;

namespace Web.My.Controllers
{
    public class HomeController : MyBaseController
    {
        public HomeController(ILogService logService, IBaseService baseService, IExtraService extraService, IStoreService storeService, IOrderService orderService, IGameService gameService, IAuctionService auctionService, IPassportService passportService, IPaymentService paymentService, IMemcachedClient memcachedClient)
            : base(logService, baseService, extraService, storeService, orderService, gameService, auctionService, passportService, paymentService, memcachedClient)
        {
        }

        /// <summary>
        /// 个人中心首页
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            var model = new HomeControllerModel.Index();
            var identity = User.Identity.Identity();

            model.User = PassportService.GetUser(identity.Id);

            return View(model);
        }

        /// <summary>
        /// 更新Avatar
        /// </summary>
        /// <returns></returns>
        public ActionResult Avatar()
        {
            return PartialView();
        }

        /// <summary>
        /// 更新Avatar
        /// </summary>
        /// <param name="bigFileName">大图标</param>
        /// <param name="middleFileName">中图标</param>
        /// <param name="smallFileName">小图标</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Avatar(HttpPostedFileBase bigFileName, HttpPostedFileBase middleFileName, HttpPostedFileBase smallFileName)
        {
            var status = false;
            var identity = User.Identity.Identity();

            if (bigFileName != null && middleFileName != null && smallFileName != null)
            {
                status = SaveAvatar(identity.Id, AvatarSize.Big, bigFileName).Status;

                SaveAvatar(identity.Id, AvatarSize.Middle, middleFileName);

                SaveAvatar(identity.Id, AvatarSize.Small, smallFileName);
            }
            var result = new SkResult<string>(status, WebHelper.GetAvatarUrl(identity.Id));
            return Json(result);
        }

        /// <summary>
        /// 更新密码
        /// </summary>
        /// <returns></returns>
        public ActionResult ChangePwd()
        {
            return PartialView();
        }

        /// <summary>
        /// 更新密码
        /// </summary>
        /// <param name="oldPwd">原密码</param>
        /// <param name="newPwd">新密码</param>
        /// <param name="confirmPwd">确认密码</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult ChangePwd(string oldPwd, string newPwd, string confirmPwd)
        {
            //var status = false;
            var identity = User.Identity.Identity();
            var result = new SkResult<string>(false);

            if (string.IsNullOrEmpty(oldPwd))
            {
                result.Data = "oldpwd";
                result.Message = "请输入原密码!";

                return Json(result);
            }
            //if (!newPwd.IsPassword())
            //{
            //    result.Data = "newpwd";
            //    result.Message = "请输入新密码!";

            //    return Json(result);
            //}
            if (newPwd != confirmPwd)
            {
                result.Data = "confirmpwd";
                result.Message = "确认密码与新密码不一致!";

                return Json(result);
            }
            var user = PassportService.GetUser(identity.Id);
            if (user != null)
            {
                var password = (newPwd.ToPassword() + PasswordSalt).ToPassword();
                if (password == user.Password)
                {
                    result.Message = "新密码不能与原密码一样!";
                }
                else
                {
                    result.Status = PassportService.ChangePassword(identity.Id, password);
                    if (!result.Status)
                    {
                        result.Message = "修改密码失败!";
                    }
                }
            }
            return Json(result);
        }

    }
}
